﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PixelToMatrix
{
    public partial class Form1 : Form
    {
        Dictionary<string, bool> status = new Dictionary<string, bool>();

        public Form1()
        {
            InitializeComponent();

            AssignClickHandlers();

            for (int i = 1; i <= 64; i++)
            {
                status.Add("button" + i.ToString(), false);
            }
        }

        private void AssignClickHandlers()
        {
            for (int i = 1; i <= 64; i++)
            {
                Button button = (Button)this.Controls.Find("button" + i.ToString(), false).First();
                button.BackColor = Color.PaleGreen;
                button.Click += button_Click;
            }
        }

        void button_Click(object sender, EventArgs e)
        {
            Button button = ((Button)sender);
            if (!status[button.Name])
            {
                status[button.Name] = true;
                button.BackColor = Color.Orange;
            }
            else
            {
                status[button.Name] = false;
                button.BackColor = Color.PaleGreen;
            }
        }

        private void button66_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 64; i++)
            {
                Button button = (Button)this.Controls.Find("button" + i.ToString(), false).First();
                button.BackColor = Color.PaleGreen;
                status[button.Name] = false;
            }
        }

        private void button65_Click(object sender, EventArgs e)
        {
            string result = string.Empty;

            for (int i = 1; i <= 64; i++)
            {
                Button button = (Button)this.Controls.Find("button" + i.ToString(), false).First();
                result += status[button.Name] ? "1" : "0";
                if (i % 8 == 0)
                {
                    result += Environment.NewLine;
                }
            }

            var resultform = new Form2();
            var textbox = (TextBox)resultform.Controls.Find("textBox1", false).First();
            textbox.Text = result;
            textbox = (TextBox)resultform.Controls.Find("textBox2", false).First();
            var array = result.Split(new string[] { Environment.NewLine }, 10, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in array)
            {
                var value = Convert.ToByte(item, 2);
                textbox.Text += "0x" + value.ToString("x2").ToUpper() + ",";
            }
            resultform.Show();
        }

        private void button67_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 64; i++)
            {
                Button button = (Button)this.Controls.Find("button" + i.ToString(), false).First();
                button.BackColor = Color.Orange;
                status[button.Name] = true;
            }
        }
    }
}
